﻿using System;
using System.Linq;
using SBACN.WebSite.Code;
using SBACN.WebSite.Models;

namespace SBACN.WebSite.Admin.AboutGuild
{
    public partial class CommonEditorPage : BasePage
    {
        public string CID { get; set; }
        public string DIC { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

            this.CID = Request.QueryString["id"];
            Dic();
            if (!IsPostBack)
            {
                Show_Connect();
            }

        }

        protected void Dic()
        {
            switch(this.CID)
            {
                case "00010001":
                    this.DIC = "GuildIntroduction  ";
                    break;
                    case "00010002":
                    this.DIC = "GuildByLaw";
                    break;
                    case "00010003":
                    this.DIC = "GuildStructure";
                    break;
                default:
                    this.DIC = "";
                    break;

            }
        }

        protected void Show_Connect()
        {
            using (var ctx = new SBACNEntities())
            {
                var result = (from NewsData in ctx.TB_News
                              where NewsData.Type == CID
                              select NewsData).FirstOrDefault();
                if (result != null)
                {
                    this.ConTitle.Text = result.Title.ToString();
                    this.Editor.Text = result.Content.ToString();
                }

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using(var ctx = new SBACNEntities())
            {
                var result = (from NewsData in ctx.TB_News
                              where NewsData.Type == CID
                              select NewsData).FirstOrDefault();
                if (result == null)
                {
                    var News = new TB_News
                    {
                        Type = CID,
                        Title = this.ConTitle.Text,
                        Content = this.Editor.Text,
                        CreateBy = "admin",
                        CreateDate = DateTime.Now
                    };
                    ctx.TB_News.AddObject(News);
                }
                else
                {
                    result.Title = this.ConTitle.Text;
                    result.Content = this.Editor.Text;
                }
                ctx.SaveChanges();
            }
            ClientScript.RegisterClientScriptBlock(this.GetType(),"AlertMsg","<script type='text/javascript'>alert('内容保存成功。');</script>");

        }
    }
}